package com.itheima.day13.thread.test15;

// 消费者类
public class Foodie implements Runnable {
    private Desk desk;

    public Foodie(Desk desk) {
        this.desk = desk;
    }

    @Override
    public void run() {
        int count = 0;
        while (true) {
            // 上锁
            synchronized(desk.getObj()){
                // 判断吃完了没
                if (desk.getCount() == 0)
                    break;
                // 判断桌子上有包子没
                if (desk.isBoo()) {
                    // 有 , 吃 ，叫
                    System.out.println("吃货正在吃包子......");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    desk.setBoo(false);
                    desk.setCount(desk.getCount() - 1);
                    System.out.println("吃完了，吃了" + (++count) + "个包子\n");
                    desk.getObj().notify();
                } else {
                    // 无 , 叫 ， 等
                    desk.getObj().notify();
                    try {
                        desk.getObj().wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}
